home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 46 / Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso / -serious- / programming / other / gtdrag / examples / dragtest.c < prev    next >
C/C++ Source or Header  |  1999-09-06  |  12KB  |  401 lines

  1. ;/* dragtest.c - Test for the gtdrag.library, 11.6.1999
  2. ;**
  3. ;** Copyright ©1999 pinc Software.
  4. ;** All rights reserved.
  5. ;**
  6. sc dragtest.c link ign=73 lib:debug.lib data=near parm=b nochkabort nostkchk strmer streq to=ram:dragtest
  7. run ram:dragtest
  8. quit
  9. */
  10.  
  11.  
  12. /* This example demonstrates the drag&drop extension of simple
  13. ** Gadtools gadgets and how the DropMessage is analysed.
  14. **
  15. ** It's a very simple example but you may use this code to produce
  16. ** one at your own.
  17. **
  18. ** Although I tried hardly to not produce any errors, I cannot give
  19. ** any guaranties. Neither I nor pinc Software is liable for any
  20. ** problems you might have with it.
  21. ** The usage of this example is on your own risk.
  22. */
  23.  
  24.  
  25. #define INTUI_V36_NAMES_ONLY
  26. #define __USE_SYSBASE
  27.  
  28. #include <exec/libraries.h>
  29. #include <exec/nodes.h>
  30. #include <exec/lists.h>
  31. #include <exec/ports.h>
  32. #include <exec/memory.h>
  33. #include <intuition/intuition.h>
  34. #include <intuition/gadgetclass.h>
  35. #include <graphics/gfx.h>
  36. #include <graphics/gfxmacros.h>
  37. #include <libraries/gadtools.h>
  38. #include <libraries/gtdrag.h>
  39. #include <libraries/asl.h>
  40. #include <dos/dos.h>
  41.  
  42. #include <clib/alib_protos.h>
  43. #include <clib/alib_stdio_protos.h>
  44. #include <clib/gtdrag_protos.h>
  45. #include <proto/exec.h>
  46. #include <proto/gadtools.h>
  47. #include <proto/intuition.h>
  48. #include <proto/graphics.h>
  49. #include <proto/layers.h>
  50. #include <proto/utility.h>
  51. #include <proto/dos.h>
  52. #include <pragmas/gtdrag_pragmas.h>
  53.  
  54. #include <stdlib.h>
  55. #include <ctype.h>
  56. #include <string.h>
  57.  
  58. void main(int, char **);
  59.  
  60. #define TLn(t) TextLength(&scr->RastPort,t,strlen(t))
  61.  
  62.  
  63. UWORD chip newImgData[52] =
  64. {
  65.         /* Plane 0 */
  66.         0x0000,0x0020,0x3FFF,0xFFE0,0x3FFF,0xFFE0,0x3FFF,0xFFE0,
  67.         0x3FFF,0xFFE0,0x3FFF,0xFFE0,0x3FFF,0xFFE0,0x3FFF,0xFFE0,
  68.         0x3FFF,0xFFE0,0x3FFF,0xFFE0,0x3FFF,0xFFE0,0x3FFF,0xFFE0,
  69.         0x7FFF,0xFFE0,
  70.         /* Plane 1 */
  71.         0xFFFF,0xFFC0,0xFFFF,0xFF80,0xF3BF,0xFF80,0xF3BF,0xFF80,
  72.         0xF5B3,0x6D80,0xF5AD,0x6D80,0xF6A1,0xAB80,0xF6AF,0xAB80,
  73.         0xF72D,0xD780,0xF733,0xD780,0xFFFF,0xFF80,0xFFFF,0xFF80,
  74.         0x8000,0x0000
  75. };
  76.  
  77. struct Image newImg = {0,0,27,13,2,newImgData,0x0003,0x0000,NULL};
  78.  
  79. struct Screen *scr;
  80. struct Window *win;
  81. struct IntuiMessage imsg;
  82. struct Library *GTDragBase;
  83. struct Gadget *glist,*lvgad[2],*gad[3];
  84. struct NewGadget ng;
  85. struct List list[2];
  86. struct Node *node;
  87. struct ImageNode in;
  88. APTR   vi;
  89. int    fontheight;
  90. BOOL   ende = FALSE;
  91.  
  92.  
  93. /**************** Requester ****************/
  94.  
  95. long DoRequestA(STRPTR t,STRPTR gads,APTR args)
  96. {
  97.   struct EasyStruct es = {sizeof(struct EasyStruct),0,"DragTest-Request",NULL,NULL};
  98.  
  99.   es.es_TextFormat = t;
  100.   es.es_GadgetFormat = gads;
  101.   return(EasyRequestArgs(scr->FirstWindow,&es,0,args));
  102. }
  103.  
  104.  
  105. void ErrorRequest(STRPTR t,...)
  106. {
  107.   DoRequestA(t,"Ok",&t+1);
  108. }
  109.  
  110.  
  111. /**************** list manipulation ****************/
  112.  
  113. void InsertAt(struct List *l,struct Node *n,int pos)
  114. {
  115.   struct Node *pn;
  116.   int    i;
  117.  
  118.   for(pn = (struct Node *)l,i = 0;i < pos;pn = pn->ln_Succ,i++);
  119.   Insert(l,n,pn);
  120. }
  121.  
  122.  
  123. void MoveTo(struct Node *n,struct List *l1,int pos1,struct List *l2,int pos2)
  124. {
  125.   struct Node *pn;
  126.   int    i;
  127.  
  128.   if (l1 == l2 && pos1 == pos2)
  129.     return;
  130.   Remove(n);
  131.  
  132.   if (l1 == l2 && pos1 < pos2)
  133.     pos2--;
  134.   for(pn = (struct Node *)l2,i = 0;i < pos2 && pn->ln_Succ;pn = pn->ln_Succ,i++);
  135.   Insert(l2,n,pn);
  136. }
  137.  
  138.  
  139. /**************** main part ****************/
  140.  
  141. void processMsg(void)
  142. {
  143.   struct IntuiMessage *msg;
  144.   struct DropMessage *dm;
  145.   int    source,target;
  146.  
  147.   while(!ende)
  148.   {
  149.     WaitPort(win->UserPort);
  150.     while(msg = GTD_GetIMsg(win->UserPort))
  151.     {
  152.       imsg = *msg;
  153.  
  154.       if (imsg.Class == IDCMP_OBJECTDROP)
  155.       {
  156.         if (dm = imsg.IAddress)
  157.         {
  158.           if (!dm->dm_Object.od_Owner && dm->dm_Target)
  159.           {
  160.             target = dm->dm_Target->GadgetID;
  161.             switch(target)
  162.             {
  163.               case 1:
  164.               case 2:
  165.                 source = dm->dm_Gadget->GadgetID;
  166.                 if (source < 3)
  167.                 {
  168.                   source--;  target--;
  169.                   GT_SetGadgetAttrs(lvgad[0],win,NULL,GTLV_Labels,~0L,TAG_END);
  170.                   GT_SetGadgetAttrs(lvgad[1],win,NULL,GTLV_Labels,~0L,TAG_END);
  171.                   if (target)
  172.                     MoveTo(dm->dm_Object.od_Object,&list[source],dm->dm_SourceEntry,&list[target],dm->dm_TargetEntry);
  173.                   else
  174.                   {
  175.                     Remove(dm->dm_Object.od_Object);
  176.                     AddTail(&list[target],dm->dm_Object.od_Object);
  177.                   }
  178.                   GT_SetGadgetAttrs(lvgad[0],win,NULL,GTLV_Labels,&list[0],TAG_END);
  179.                   GT_SetGadgetAttrs(lvgad[1],win,NULL,GTLV_Labels,&list[1],TAG_END);
  180.                 }
  181.                 else
  182.                 {
  183.                   target--;
  184.                   GT_SetGadgetAttrs(lvgad[target],win,NULL,GTLV_Labels,~0L,TAG_END);
  185.                   if (node = AllocMem(sizeof(struct Node),MEMF_CLEAR | MEMF_PUBLIC))
  186.                   {
  187.                     node->ln_Name = in.in_Name;
  188.                     InsertAt(&list[target],node,dm->dm_TargetEntry);
  189.                   }
  190.                   GT_SetGadgetAttrs(lvgad[target],win,NULL,GTLV_Labels,&list[target],TAG_END);
  191.                 }
  192.                 break;
  193.               case 3:
  194.                 DisplayBeep(NULL);
  195.                 break;
  196.               case 4:
  197.               case 5:
  198.                 source = dm->dm_Gadget->GadgetID-1;
  199.                 GT_SetGadgetAttrs(lvgad[source],win,NULL,GTLV_Labels,~0L,TAG_END);
  200.                 if (target == 4)
  201.                 {
  202.                   if (node = AllocMem(sizeof(struct Node),MEMF_CLEAR | MEMF_PUBLIC))
  203.                   {
  204.                     node->ln_Name = ((struct ImageNode *)dm->dm_Object.od_Object)->in_Name;
  205.                     AddTail(&list[source],node);
  206.                   }
  207.                 }
  208.                 else
  209.                 {
  210.                   Remove((struct Node *)dm->dm_Object.od_Object);
  211.                   FreeMem(dm->dm_Object.od_Object,sizeof(struct Node));
  212.                 }
  213.                 GT_SetGadgetAttrs(lvgad[source],win,NULL,GTLV_Labels,&list[source],TAG_END);
  214.                 break;
  215.             }
  216.           }
  217.           else if (!dm->dm_Object.od_Owner)
  218.           {
  219.             DisplayBeep(NULL);   // unsupported drag?!
  220.           }
  221.           else if (!stricmp(dm->dm_Object.od_Owner,"dragtest"))
  222.           {
  223.             char t[256];
  224.  
  225.             if (GTD_GetString(&dm->dm_Object,t,256))
  226.               ErrorRequest("I do not want a node from a clone of mine.\nEspecially, I don't like \"%s\".",t);
  227.           }
  228.           else if (!strcmp(dm->dm_Object.od_Owner,"intuition"))
  229.             ErrorRequest("An object from a custom gadget?\nThat's nothing for me.",dm->dm_Object.od_Owner);
  230.           else
  231.             ErrorRequest("An object from %s?\nSounds interesting.",dm->dm_Object.od_Owner);
  232.         }
  233.         else
  234.           DisplayBeep(NULL);
  235.       }
  236.       GTD_ReplyIMsg(msg);
  237.  
  238.       switch(imsg.Class)
  239.       {
  240.         case IDCMP_GADGETDOWN:
  241.           break;
  242.         case IDCMP_GADGETUP:
  243.           switch(((struct Gadget *)imsg.IAddress)->GadgetID)
  244.           {
  245.             case 3:
  246.               GT_SetGadgetAttrs(lvgad[0],win,NULL,GTLV_Labels,~0L,TAG_END);
  247.               if (node = AllocMem(sizeof(struct Node),MEMF_CLEAR | MEMF_PUBLIC))
  248.               {
  249.                 node->ln_Name = in.in_Name;
  250.                 AddTail(&list[0],node);
  251.               }
  252.               GT_SetGadgetAttrs(lvgad[0],win,NULL,GTLV_Labels,&list[0],TAG_END);
  253.               break;
  254.           }
  255.           break;
  256.         case IDCMP_CLOSEWINDOW:
  257.           ende = TRUE;
  258.           break;
  259.       }
  260.     }
  261.   }
  262. }
  263.  
  264. const STRPTR txt[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
  265.  
  266. struct Window *initWindow(void)
  267. {
  268.   struct Window *win;
  269.  
  270.   gad[0] = CreateContext(&glist);
  271.  
  272.   ng.ng_LeftEdge = 8;
  273.   ng.ng_TopEdge = fontheight+7;
  274.   ng.ng_Width = 192;
  275.   ng.ng_Height = 11*fontheight+4;
  276.   ng.ng_VisualInfo = vi;
  277.   ng.ng_TextAttr = scr->Font;
  278.   ng.ng_GadgetText = NULL;
  279.   ng.ng_UserData = NULL;
  280.   ng.ng_GadgetID = 1;
  281.   lvgad[0] = CreateGadget(LISTVIEW_KIND,gad[0],&ng,GTLV_Labels,&list[0],TAG_END);
  282.  
  283.   ng.ng_LeftEdge = 206;
  284.   ng.ng_Width = 186;
  285.   ng.ng_GadgetText = NULL;
  286.   ng.ng_GadgetID++;
  287.   lvgad[1] = CreateGadget(LISTVIEW_KIND,lvgad[0],&ng,GTLV_Labels,&list[1],TAG_END);
  288.  
  289.   ng.ng_TopEdge += ng.ng_Height+3;
  290.   ng.ng_LeftEdge = 8;
  291.   ng.ng_Height = fontheight+4;
  292.   ng.ng_Width = TLn("New")+16;
  293.   ng.ng_Flags = PLACETEXT_IN;
  294.   ng.ng_GadgetText = "New";
  295.   ng.ng_GadgetID++;
  296.   gad[0] = CreateGadget(BUTTON_KIND,lvgad[1],&ng,GA_Immediate,TRUE,TAG_END);
  297.  
  298.   ng.ng_LeftEdge += ng.ng_Width+6;
  299.   ng.ng_Width = TLn("Copy")+16;
  300.   ng.ng_GadgetText = "Copy";
  301.   ng.ng_GadgetID++;
  302.   gad[1] = CreateGadget(BUTTON_KIND,gad[0],&ng,TAG_END);
  303.  
  304.   ng.ng_LeftEdge += ng.ng_Width+6;
  305.   ng.ng_Width = TLn("Delete")+16;
  306.   ng.ng_GadgetText = "Delete";
  307.   ng.ng_GadgetID++;
  308.   gad[2] = CreateGadget(BUTTON_KIND,gad[1],&ng,TAG_END);
  309.  
  310.   if (win = OpenWindowTags(NULL,WA_Title,   "gtdrag - Test",
  311.                                 WA_Flags,   WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_ACTIVATE,
  312.                                 WA_IDCMP,   IDCMP_CLOSEWINDOW | DRAGIDCMP,
  313.                                 WA_Width,   400,
  314.                                 WA_Height,  fontheight*13+23,
  315.                                 WA_Gadgets, glist,
  316.                                 TAG_END))
  317.   {
  318.     GT_RefreshWindow(win,NULL);
  319.     GTD_AddGadget(LISTVIEW_KIND,lvgad[0],win,GTDA_InternalType, 1,
  320.                                              GTDA_NoPosition,   TRUE,
  321.                                              GTDA_Type,         ODT_NODE,
  322.                                              TAG_END);
  323.     GTD_AddGadget(LISTVIEW_KIND,lvgad[1],win,GTDA_InternalType,1,
  324.                                              GTDA_Same,        TRUE,
  325.                                              GTDA_Type,        ODT_NODE,
  326.                                              TAG_END);
  327.     GTD_AddGadget(BUTTON_KIND,gad[0],win,GTDA_Object,      &in,
  328.                                          GTDA_Image,       in.in_Image,
  329.                                          GTDA_InternalType,2,
  330.                                          GTDA_AcceptTypes, 0,
  331.                                          GTDA_Width,       newImg.Width,
  332.                                          GTDA_Height,      newImg.Height,
  333.                                          TAG_END);
  334.     GTD_AddGadget(BUTTON_KIND,gad[1],win,GTDA_AcceptTypes,1,TAG_END);
  335.     GTD_AddGadget(BUTTON_KIND,gad[2],win,GTDA_AcceptTypes,1,TAG_END);
  336.     GTD_AddWindow(win,TAG_END);
  337.     return(win);
  338.   }
  339.   return(NULL);
  340. }
  341.  
  342.  
  343. void cleanup(void)
  344. {
  345.   int i;
  346.  
  347.   for(i = 0;i < 2;i++)
  348.     while(node = RemHead(&list[i]))
  349.       FreeMem(node,sizeof(struct Node));
  350. }
  351.  
  352.  
  353. void init(void)
  354. {
  355.   int i;
  356.  
  357.   NewList(&list[0]);
  358.   NewList(&list[1]);
  359.  
  360.   for(i = 0;i<20;i++)
  361.   {
  362.     if (node = AllocMem(sizeof(struct Node),MEMF_CLEAR | MEMF_PUBLIC))
  363.     {
  364.       node->ln_Name = txt[i];
  365.       AddTail(&list[0],node);
  366.     }
  367.   }
  368.   in.in_Name = "** New **";
  369.   in.in_Image = &newImg;
  370. }
  371.  
  372.  
  373. void main(int argc, char **argv)
  374. {
  375.   if (GTDragBase = OpenLibrary("gtdrag.library",3))
  376.   {
  377.     if (GTD_AddApp("dragtest",GTDA_NewStyle,TRUE,TAG_END))
  378.     {
  379.       if (scr = LockPubScreen(NULL))
  380.       {
  381.         vi = GetVisualInfo(scr,TAG_END);
  382.         fontheight = scr->Font->ta_YSize;
  383.         init();
  384.  
  385.         if (win = initWindow())
  386.         {
  387.           processMsg();
  388.           CloseWindow(win);
  389.         }
  390.         FreeGadgets(glist);
  391.  
  392.         cleanup();
  393.         FreeVisualInfo(vi);
  394.         UnlockPubScreen(NULL,scr);
  395.       }
  396.       GTD_RemoveApp();
  397.     }
  398.     CloseLibrary(GTDragBase);
  399.   }
  400. }
  401.